package com.iflytek.web.sevlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;
import com.iflytek.domain.ProjectType;
import com.iflytek.domain.PageBean;
import com.iflytek.domain.Project;
import com.iflytek.domain.Hometype;
import com.iflytek.service.ProjectService;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/project")
public class ProjectServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	//private Logger logger=LogManager.getRootLogger();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectServlet() {
        super();
    }

	// 获取导航栏的项目名称
	public void getProjectName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		ProjectService ps=(ProjectService) ac.getBean("projectService");
		List<Project> projectList = ps.getAllProject();
		Gson gson = new Gson();
		String json = gson.toJson(projectList);
		response.getWriter().write(json);
	}

	// 通过pid获取项目下的分类并跳转到项目分类页面
	public void getProjectCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		ProjectService ps=(ProjectService) ac.getBean("projectService");
		
		String pid = request.getParameter("pid");
		String currentPageStr = request.getParameter("currentPage");
		if (currentPageStr == null) {
			currentPageStr = "1";
		}
		int currentPage = Integer.parseInt(currentPageStr);
		ProjectType projectType = ps.getProjectTypeByProjectId(pid);
		int currentCount = projectType.getPt_currentCount();
		String pt_id=projectType.getPt_id();
		PageBean pageBean = ps.getPageProjectByPid(pid, currentPage, currentCount,pt_id);
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("pid", pid);
		String url=projectType.getPt_url();
		request.getRequestDispatcher(url).forward(request, response);
	}

	// 通过Pid跳转到项目下的产品详情页面
	public void intoHometypeInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		ProjectService ps=(ProjectService) ac.getBean("projectService");
		
		String htid = request.getParameter("htid");
		Hometype hometype = ps.getHometypeById(htid);
		request.setAttribute("hometype", hometype);
		String projectId=hometype.getProject_id();
		Project project = ps.getProjectByPid(projectId);
		String url=project.getProject_url();
		request.getRequestDispatcher(url).forward(request, response);
	}


	//自动补全导航栏
	public void autoSearchWord(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		ProjectService ps=(ProjectService) ac.getBean("projectService");
		String search=request.getParameter("search");
		List<String> searchList=ps.getAutoSearchWord(search);
		Gson gson = new Gson();
		String json = gson.toJson(searchList);
		response.getWriter().write(json);
	}

}
